package  
{
	import flash.utils.ByteArray;
	/**
	 * ...
	 * @author Sang Tian
	 */
	public class StringBuilder 
	{
		
		private var _buffer:ByteArray = new ByteArray();
		private var _mark:int = 0;
		
		public function appendChar(charCode:uint):StringBuilder
		{
			//trace("Append:", charCode);
			_buffer.writeByte(charCode);
			return this;
		}
		
		public function appendString(s:String, mark:Boolean = true):StringBuilder
		{
			_buffer.writeUTFBytes(s);
			if (mark)
			{
				this.mark();
			}
			return this;
		}
		
		public function mark():void
		{
			_mark = _buffer.position;
		}
		
		public function backspace():int
		{
			if (_buffer.position <= _mark)
				return 0;
			_buffer.position--;
			var b:int = _buffer.readByte();
			//trace("Backspace:", b);
			_buffer.position--;
			_buffer.writeByte(0);
			_buffer.position--;
			return b;
		}
		
		public function toString():String
		{
			return _buffer.toString();
		}
	}

}